package cn.hwadee.muying.recommend;

import cn.hwadee.muying.recommend.util.ExcelUtil;
import cn.hwadee.muying.recommend.util.FileUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建人 skiner
 * @创建时间 2018/8/14
 * @描述 将文本里面的字符串转换
 */
public class Translate {
    private static Logger log = LoggerFactory.getLogger(Translate.class);
    /**
     *
     * @param str 待转换的字符串
     * @param map key为需转换的字符串，value为目标字符串
     * @return 转换后的字符串
     */
    static Map<String, List<String>> work( Map<String, List<String>> str,Map<String, String> map) {
        Map<String, List<String>> retMap = new HashMap<>();
        for (Map.Entry<String, List<String>> entry : str.entrySet()) {
            List<String> list = entry.getValue();
            String key = map.get(entry.getKey());
            List<String> l = new ArrayList<>();
            for(String s:list){
                l.add(map.get(s));
            }
            retMap.put(key,l);
        }
        return retMap;
    }

    public static void main(String[] args) throws IOException {
        Map<String, String> map = new HashMap<>();
        Map<String, List<String>> list = new HashMap<>();
        //读取数据
        File file = new File("d:/worker1.xls");
        Workbook wb = new HSSFWorkbook(new FileInputStream(file));
        Sheet sheet = wb.getSheetAt(0);
        for (Row row : sheet) {
            map.put(row.getCell(0).getStringCellValue(), row.getCell(1).getStringCellValue());
        }
        wb.close();
        file = new File("d:/ret(3).xls");
        wb = ExcelUtil.getWorkbok(file);
        sheet = wb.getSheetAt(0);
        for(Row row:sheet){
            Cell cell = row.getCell(0);
            cell.setCellType(Cell.CELL_TYPE_STRING);
            String key = cell.getStringCellValue();

            List<String> t = new ArrayList<>();
            for(int i = 1; i <= 10; i++){
                cell = row.getCell(i);
                cell.setCellType(Cell.CELL_TYPE_STRING);
                t.add(cell.getStringCellValue());
            }
            list.put(key, t);
        }
        wb.close();
        log.info("读完");
        //转换
        Map<String, List<String>> ret = work(list,map);
        log.info("转完");
        //保存
        wb = new HSSFWorkbook();
        sheet = wb.createSheet();
        int i = 0;
        for(Map.Entry<String,List<String>> entry: ret.entrySet()){
            Row row = sheet.createRow(i++);
            Cell cell = row.createCell(0);
            cell.setCellValue(entry.getKey());
            List<String> li = entry.getValue();
            for(int j = 1; j <= 10; j++){
                String str = li.get(j-1);
                cell = row.createCell(j);
                cell.setCellValue(str);
            }
        }
        file = FileUtil.get("d:/","ui","xls");
        wb.write(new FileOutputStream(file));
        log.info("存完");
    }
}
